package jp.co.apl.haven_enquete.android.activity;

import jp.co.apl.haven_enquete.android.R;
import jp.co.apl.haven_enquete.android.dao.AnswerDataDao;
import jp.co.apl.haven_enquete.android.worker.AnswerUploadWorker;
import jp.dev7.android.util.Activities;
import jp.dev7.android.util.Toasts;
import jp.dev7.android.worker.Worker;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;

public class Upload extends Activity {

    private AnswerDataDao aDao = new AnswerDataDao(this);
    private ActivityCommon activityCommon = new ActivityCommon(this);
    private AnswerUploadWorker worker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.upload);
        activityCommon.setActionBar("送信");
        Activities.findViewById(this, R.id.actionBar_nextButton).setVisibility(
            View.INVISIBLE);

        Activities.setClickListnerToButton(
            this,
            R.id.upload_upload,
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    startUpload();
                }
            });
    }

    private void startUpload() {
        showDialog(0);

        worker = new AnswerUploadWorker(aDao.findAll());
        worker.execute(new Handler() {
            @Override
            public void handleMessage(Message msg) {
                removeDialog(0);
                if (!worker.isCanceled()) {
                    if (msg.getData().get(Worker.RESULT) != null
                        && msg.getData().getBoolean(Worker.RESULT)) {
                        Toasts.show(Upload.this, "success");
                    } else {
                        Toasts.show(Upload.this, "fail");
                    }
                }
            }
        });
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = new ProgressDialog(this);
        dialog.setTitle("通信中...");
        dialog.setOnCancelListener(new Dialog.OnCancelListener() {
            @Override
            public void onCancel(DialogInterface dialog) {
                if (worker != null) {
                    worker.cancel();
                }
                dialog.cancel();
            }
        });
        dialog.setOnDismissListener(new Dialog.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                if (worker != null) {
                    worker.cancel();
                }
                dialog.dismiss();
            }
        });
        return dialog;
    }

    @Override
    protected void onDestroy() {
        activityCommon = null;
        aDao = null;
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        return activityCommon.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        super.onOptionsItemSelected(item);
        return activityCommon.onOptionsItemSelected(item);
    }
}
